package com.etalk.crm.controller;

import com.etalk.crm.service.StudentReportCustomizeService;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.Date;

/**
 * @author Jordan
 */
@Controller
@RequestMapping("/student/report")
public class StudentReportController {
    private static final Logger logger = LogManager.getLogger(StudentReportController.class);
    @Resource
    private StudentReportCustomizeService studentReportCustomizeService;

    @Value("${web.crm.h5.url}")
    private String h5Url;

    /**
     * 初始化自定义时间段学习报表的页面
     * @param model 页面数据
     * @param personId 学员id
     * @return 页面
     */
    @RequestMapping(value = "/init/{personId}",method = {RequestMethod.GET})
    public String initStudentReportByCustomize(Model model,@PathVariable("personId") int personId,String tokenString){
        model.addAttribute("webUrl",h5Url);
        model.addAttribute("personId",personId);
        model.addAttribute("tokenString",tokenString);
        return "student/student_report_customize";
    }
    /**
     * 生成自定义时间段学习报表
     * @param model 页面数据
     * @param personId 用户id
     * @param startDate 开始时间
     * @param endDate 结束时间
     * @param session 当前会话数据
     * @return 页面
     */
    @RequestMapping(value = "/create",method = {RequestMethod.POST})
    public String createStudentReportByCustomize(Model model, @RequestParam int personId, @RequestParam String startDate, @RequestParam String endDate,String tokenString, HttpSession session) throws ParseException {
        model.addAttribute("startDate",startDate);
        model.addAttribute("endDate",endDate);
        try {
            int num=studentReportCustomizeService.createReportByCustomize(personId, DateUtils.parseDate(startDate,"yyyy-MM-dd HH:mm:ss"), DateUtils.parseDate(endDate,"yyyy-MM-dd HH:mm:ss"),session.getAttribute("loginname").toString());
            if (num>0){
                model.addAttribute("status",1);
            }else {
                model.addAttribute("status", 0);
                switch (num){
                    case -1:
                        model.addAttribute("errMsg", "选择的时间段学习报表已存在");
                        break;
                    default:
                        model.addAttribute("errMsg", "创建报表失败，时间段内没有课程数据");
                }
            }
        }catch (DuplicateKeyException e){
            model.addAttribute("status",0);
            model.addAttribute("errMsg","选择的时间段学习报表以存在");
            logger.error(e);
        }

        return this.initStudentReportByCustomize(model,personId,tokenString);
    }
}
